var HtmlToJson = require('../wxParse/html2json.js');

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatUTC(utc){
  var date = new Date(utc)
  return date
}

//时间格式化HH:ss  
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
} 

//格式化开源资讯数据
function formatReadHub(data){
  var transData = HtmlToJson.html2json(data, 'news');
  var data = [];
  for (var i = 0; i < transData.nodes.length-1; i++) {
    var item = {};
    var rootDiv = transData.nodes[i].nodes[0];
    for(var j=0;j<rootDiv.nodes.length;j++){
      var secondDiv = rootDiv.nodes[j];
      if (secondDiv.tag=="a"){
        //console.log(secondDiv.nodes[0].nodes[0].text);
        item.title = secondDiv.nodes[0].nodes[0].text;
      }
      if (secondDiv.classStr.indexOf("summary")>-1){
        //console.log(secondDiv.nodes[0].text);
        item.summary = secondDiv.nodes[0].text;
      }
    }
    data.push(item);
  }
  return data;
}


function toastNetError(){
  wx.showToast({
    title: '网络请求出错',
    icon: 'none',
    duration: 500
  })
}

//正则表达式判断
function regxCheck(value,regx){
  if (regx.test(value)) {
    return true;
  }else {
    return false;
  }
}

//判断是否是字母
function checkLetter(value) {
  var Regx = /^[A-Za-z]*$/;
  return regxCheck(value,Regx);
}

//判断是否是小写字母
function checkLetterLower(value) {
  var Regx = /^[a-z]*$/;
  return regxCheck(value, Regx);
}

//判断是否是数字
function checkNum(value) {
  var Regx = /^[0-9]*$/;
  return regxCheck(value, Regx);
}

//判断是否是英文符号
function checkSign(value){
  var regEn = /[`~!@#$%^&*()_+<>?:\"{},.\/;'[\]\s]/;
  return regxCheck(value, regEn);
}



function savePicToAlbum(tempFilePath) {
  let that = this;
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success(res) {
                wx.showToast({
                  title: '保存成功'
                });
              },
              fail(res) {
                console.log(res);
              }
            })
          },
          fail() {
            // 用户拒绝授权,打开设置页面
            wx.openSetting({
              success: function (data) {
                console.log("openSetting: success");
              },
              fail: function (data) {
                console.log("openSetting: fail");
              }
            });
          }
        })
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
            });
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    },
    fail(res) {
      console.log(res);
    }
  })
}


module.exports = {
  formatTime: formatTime,
  formatReadHub:formatReadHub,
  toastNetError:toastNetError,
  savePicToAlbum: savePicToAlbum,
  checkLetter: checkLetter,
  checkNum: checkNum,
  checkSign: checkSign,
  checkLetterLower: checkLetterLower
};




